package com.jy.adscm.operation.cg.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.jy.adscm.bean.system.JyUser;
import com.jy.adscm.exception.AdscmSystemException;
import com.jy.adscm.operation.aftersales.bean.ShylMaterial;
import com.jy.adscm.operation.aftersales.mapper.mysql.ShylMaterialMapper;
import com.jy.adscm.operation.cg.bean.*;
import com.jy.adscm.operation.cg.mapper.mysql.*;
import com.jy.adscm.operation.cg.service.CgTaskDetailService;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.jy.adscm.operation.mscyb.bean.JqylMaterial;
import com.jy.adscm.operation.mscyb.mapper.mysql.JqylMaterialMapper;
import com.jy.adscm.security.utils.Token;
import com.jy.adscm.system.mapper.mysql.JyUserMapper;
import com.jy.adscm.utils.AdscmBaseUtils;
import com.jy.adscm.utils.AdscmDateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 采购详情 服务实现类
 * </p>
 *
 * @author zyl
 * @since 2019-07-07
 */
@Service
public class CgTaskDetailServiceImpl extends ServiceImpl<CgTaskDetailMapper, CgTaskDetail> implements CgTaskDetailService {
   @Autowired
    JyUserMapper jyUserMapper;
   @Autowired
   CgMaterialMapper cgMaterialMapper;
   @Autowired
   JqylMaterialMapper jqylMaterialMapper;
   @Autowired
    CgTaskMapper cgTaskMapper;

   @Autowired
    ShylMaterialMapper shylMaterialMapper;
   @Autowired
    CgPurchasePlanMapper cgPurchasePlanMapper;
    @Override
    public List<CgTaskDetail> selectByTid(Long tid) {
        Wrapper<CgTaskDetail> wrapper =new EntityWrapper<>();
        wrapper.eq("tid",tid);
        List<CgTaskDetail> cgTaskDetails = super.selectList(wrapper);
        for (CgTaskDetail cgTaskDetail : cgTaskDetails) {
            if (AdscmBaseUtils.getInstance().isNotEmpty(cgTaskDetail.getBomName(),null)){
                CgMaterial cgMaterial = cgMaterialMapper.selectById(cgTaskDetail.getBomName());
                cgTaskDetail.setBomNameName(cgMaterial.getName());
            }
            if (AdscmBaseUtils.getInstance().isNotEmpty(cgTaskDetail.getCreator(),null)){
                JyUser jyUser = jyUserMapper.selectById(cgTaskDetail.getCreator());
                cgTaskDetail.setCreatorName(jyUser.getName());
            }
        }

        return cgTaskDetails;
    }
    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean updateCgTaskDetail(CgTaskDetail cgTaskDetail, Token token) {
        CgTask cgTask = cgTaskMapper.selectById(cgTaskDetail.getTid());
        CgTaskDetail oldcgTaskDetail = super.selectById(cgTaskDetail.getId());

        if (AdscmBaseUtils.getInstance().isNotEmpty(cgTaskDetail.getActPrice(),null)){
          if (AdscmBaseUtils.getInstance().isNotEmpty(cgTaskDetail.getActNums(),null)){
              cgTaskDetail.setActTotal(cgTaskDetail.getActPrice()*cgTaskDetail.getActNums());
          }
        }



        if (!AdscmBaseUtils.getInstance().isNotEmpty(oldcgTaskDetail.getActNums(),null)){
            if (AdscmBaseUtils.getInstance().isNotEmpty(cgTaskDetail.getActNums(),null)){
                cgTaskDetail.setIsBuy(1);
                cgTaskDetail.setActPrice(cgTaskDetail.getPrice());
                //对应生成原料库的生产计划机器生产
                if (cgTaskDetail.getUse()==1){
                    JqylMaterial jqylMaterial=new JqylMaterial();
                    jqylMaterial.setTaskcode( Long.valueOf(cgTask.getMakeNo()));
                    jqylMaterial.setBatchnums(new SimpleDateFormat("yyMMdd").format(new Date()));
                    jqylMaterial.setResourcename(cgTaskDetail.getBomName().toString());
                    CgMaterial cgMaterial = cgMaterialMapper.selectById(cgTaskDetail.getBomName());
                    jqylMaterial.setTypenums(cgMaterial.getType());
                    jqylMaterial.setTaskreleaseperson(token.getUser().getId().toString());
                    jqylMaterial.setTaskreleaseaddtime(new Date());
                    jqylMaterial.setTaskperformperson(token.getUser().getId().toString());
                    //计划到达时间
                    jqylMaterial.setTaskstartorendtime(cgTaskDetail.getPlanArriveTime());
                    //启动任务
                    jqylMaterial.setIstaskstart("1");
                    jqylMaterial.setTaskstartaddtime(new Date());
                    //已经收货
                    jqylMaterial.setIstaskbegin("1");
                    //实际收货时间
                    jqylMaterial.setTaskbegintime(new Date());
                    jqylMaterial.setTaskbeginaddtime(new Date());
                    jqylMaterial.setTaskfinishstate(0);
                    jqylMaterial.setTaskstate(0);
                    jqylMaterial.setTasknums(Double.valueOf(String.valueOf(cgTaskDetail.getNums())));
                    jqylMaterial.setActualcompletenums(Double.valueOf(String.valueOf(cgTaskDetail.getActNums())));
                    jqylMaterial.setRemainingnums(cgTaskDetail.getActNums());
                    jqylMaterial.setIstaskend("0");
                    jqylMaterial.setIsreviewtask("0");
                    jqylMaterial.setReviewresult("-1");
                    jqylMaterial.setTaskstate(1);
                    jqylMaterial.setPrice(cgTaskDetail.getActPrice());
                    jqylMaterial.setTotals(cgTaskDetail.getActTotal());
                    jqylMaterial.setBusinesstype(cgTask.getSupplier().toString());
                    jqylMaterialMapper.insert(jqylMaterial);
                }
                //售后服务
                if (cgTaskDetail.getUse()==2){
                  ShylMaterial shylMaterial =new ShylMaterial();
                    shylMaterial.setTaskcode( cgTask.getMakeNo());
                    shylMaterial.setBatchnums(new SimpleDateFormat("yyMMdd").format(new Date()));
                    shylMaterial.setResourcename(cgTaskDetail.getBomName().toString());
                    CgMaterial cgMaterial = cgMaterialMapper.selectById(cgTaskDetail.getBomName());
                    shylMaterial.setTypenums(cgMaterial.getType());
                    shylMaterial.setTaskreleaseperson(token.getUser().getId().toString());
                    shylMaterial.setTaskreleaseaddtime(new Date());
                    shylMaterial.setTaskperformperson(token.getUser().getId().toString());
                    //计划到达时间
                    shylMaterial.setTaskstartorendtime(cgTaskDetail.getPlanArriveTime());
                    //启动任务
                    shylMaterial.setIstaskstart(1);
                    shylMaterial.setTaskstartaddtime(new Date());
                    //已经收货
                    shylMaterial.setIstaskbegin(1);
                    //实际收货时间
                    shylMaterial.setTaskbegintime(new Date());
                    shylMaterial.setTaskbeginaddtime(new Date());
                    shylMaterial.setTaskfinishstate(0);
                    shylMaterial.setTaskstate(0);
                    shylMaterial.setTasknums(Double.valueOf(String.valueOf(cgTaskDetail.getNums())));
                    shylMaterial.setActualcompletenums(Double.valueOf(String.valueOf(cgTaskDetail.getActNums())));
                    shylMaterial.setRemainingnums(Double.valueOf(String.valueOf(cgTaskDetail.getActNums())));
                    shylMaterial.setIstaskend(0);
                    shylMaterial.setIsreviewtask(0);
                    shylMaterial.setReviewresult(-1);
                    shylMaterial.setTaskstate(1);
                    shylMaterial.setPrice(cgTaskDetail.getActPrice());
                    shylMaterial.setTotals(cgTaskDetail.getActTotal());
                    shylMaterial.setBusinesstype(cgTask.getSupplier().toString());
                    shylMaterialMapper.insert(shylMaterial);

                }

                cgTaskDetail.setKfPerson(token.getUser().getId().toString());
                cgTaskDetail.setArriveTime(new Date());
            }
        }

        boolean b = super.updateById(cgTaskDetail);
        //如果这个里面的都到货了外面任务就默认到货
        Wrapper<CgTaskDetail> cgTaskDetailWrapper=new EntityWrapper<>();
        cgTaskDetailWrapper.eq("tid",cgTaskDetail.getTid());
        List<CgTaskDetail> cgTaskDetails = super.selectList(cgTaskDetailWrapper);
        Integer temp=0;
        for (CgTaskDetail taskDetail : cgTaskDetails) {
            if (taskDetail.getIsBuy()==0){
                temp=1;
                break;
            }
        }
        if (temp==0){
            //已经收货完成
            cgTask.setKfPerson(token.getUser().getId().toString());
            cgTask.setIsArrive(1);
            cgTask.setArriveTime(new Date());
            cgTask.setArriveAddTime(new Date());
            cgTaskMapper.updateById(cgTask);
        }

        return b;
    }
    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean deleteCgTaskDetail(Integer id, Token token)throws AdscmSystemException {
        CgTaskDetail cgTaskDetail = super.selectById(id);
        if (AdscmBaseUtils.getInstance().isNotEmpty(cgTaskDetail.getActNums(),null)){
            throw new AdscmSystemException("已经有实际数量，收货了，不能删除");
        }
        CgTask cgTask = cgTaskMapper.selectById(cgTaskDetail.getTid());

        if (cgTask.getIsStockUp()==1){
            throw new AdscmSystemException("已经通知供应商备货，不能删除");
        }
        Wrapper<CgPurchasePlan> cgPurchasePlanWrapper=new EntityWrapper<>();
        cgPurchasePlanWrapper.eq("make_no", Integer.valueOf(cgTask.getMakeNo()));
        cgPurchasePlanWrapper.eq("bid",cgTaskDetail.getBomName());
        List<CgPurchasePlan> cgPurchasePlans = cgPurchasePlanMapper.selectList(cgPurchasePlanWrapper);
        if (cgPurchasePlans!=null && cgPurchasePlans.size()>0){
            CgPurchasePlan cgPurchasePlan = cgPurchasePlans.get(0);
            cgPurchasePlan.setState(0);
            cgPurchasePlanMapper.updateById(cgPurchasePlan);
        }else {
            throw new AdscmSystemException("未查找到审核计划，不能删除");
        }
        boolean b = super.deleteById(id.longValue());
        return b;
    }
}
